<template>
  <el-dialog
    v-model="dialogVisible"
    :append-to-body="true"
    :close-on-click-modal="false"
    width="800px"
    v-bind="$attrs"
    @closed="closedfn"
    @open="openfn"
  >
    <div>
      <el-input
        v-model="pcssearch"
        :placeholder="$z.$t('区域名称')"
        style="width: 200px; margin-right: 40px"
      />
      <el-button type="primary" @click="searchfn">
        {{ $z.$t('搜索') }}
      </el-button>
      <el-button @click="delsearchfn">{{ $z.$t('清空') }}</el-button>
    </div>
    <div style="height: 500px; padding: 20px 0">
      <el-scrollbar>
        <pcstree
          v-if="treefalg"
          ref="tree"
          :expandedkeys="expandedkeys"
          :pcsid="pcsid"
          :searchdata="searchdata"
          show-checkbox
        />
      </el-scrollbar>
    </div>
    <template #footer>
      <slot></slot>
    </template>
  </el-dialog>
</template>

<script>
  import pcstree from '@/components/citypcstree/index.vue'
  export default {
    name: 'Formpcs',
    components: {
      pcstree,
    },
    props: {},

    data() {
      return {
        dialogVisible: false,
        pcssearch: '',
        treefalg: false,
        expandedkeys: [],
        searchdata: [],
        pcsid: {},
        all: false,
      }
    },
    computed: {},
    created() {},
    mounted() {},
    methods: {
      async openfn() {
        this.pcssearch = ''

        await this.expandedkeysfn()
        this.treefalg = true
      },
      closedfn() {
        this.treefalg = false
      },
      async expandedkeysfn() {
        if (!this.pcsid.id) {
          return
        }
        let datas = await this.$store.dispatch('citystation/getstationpidsfn', {
          pcsid: this.pcsid.id,
        })
        this.expandedkeys = datas.map((e) => {
          return e.pid
        })
      },
      async showEdit(falg, item = {}, v = '') {
        v = String(v)
        if (item.all == 1) {
          this.pcsid = v
        } else if (v == '') {
          this.pcsid = {}
        } else {
          let array = v.split(','),
            pcsid = []
          console.log(array)
          for (let index = 0; index < array.length; index++) {
            const element = array[index]
            let pcsdata = await this.$store.dispatch(
              'citystation/getstationone',
              element
            )
            pcsid = pcsid.concat(this.$z.deepClone(pcsdata))
          }
          this.pcsid = pcsid.length == 0 ? {} : pcsid[0]
        }

        this.all = item.all
        this.dialogVisible = falg
      },
      getCurrentNode() {
        return this.$refs.tree.getCurrentNode()
      },
      async delsearchfn() {
        this.treefalg = false
        this.expandedkeys = []
        this.searchdata = []
        this.$nextTick(() => {
          this.treefalg = true
        })
      },
      async searchfn() {
        if (this.pcssearch == '') {
          return
        }
        this.treefalg = false
        let StationPid = await this.$z.request('/CityStation', {
          pcs: this.pcssearch,
        })

        if (StationPid.length == 0) {
          this.$message({
            showClose: true,
            message: '没有搜索结果',
            type: 'warning',
          })
          this.treefalg = true
          return
        }
        let datas = await this.$store.dispatch(
          'citystation/getstationpidsall',
          StationPid
        )

        this.searchdata = datas
        this.expandedkeys = datas.map((e) => {
          return e.pid
        })
        // console.log(this.expandedkeys)
        this.treefalg = true
      },
    },
  }
</script>
<style></style>
